package cn.shenlong.portal.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.shenlong.common.Page;
import cn.shenlong.portal.domain.User;
import cn.shenlong.portal.service.UserService;

@Controller
@RequestMapping("/user/admin")
public class UserAController {

	@Autowired
	private UserService userService;

	@RequestMapping("/list")
	public String list(String searchProperty, String searchValue,
			Integer pageNumber, Integer pageSize, HttpServletRequest request,
			ModelMap model) {
		if (pageNumber == null) {
			pageNumber = 1;
		}
		if (pageSize == null) {
			pageSize = 20;
		}
		User user = new User();
		user.setUserName(searchValue);
		Page p = userService.findPageListByCriteria(user, null, pageNumber,
				pageSize);
		model.put("page", p);
		model.put("user", user);

		return "/server/user/list";
	}

	@ResponseBody
	@RequestMapping(value = "/ajaxRegistName")
	public Map<String, Object> ajaxRegistName(String registName,
			HttpServletRequest request, HttpServletResponse response) {

		List<User> result = userService.findUserByRegistName(registName);
		Map<String, Object> resultMap = new HashMap<String, Object>();

		if (result != null && result.size() > 0) {
			resultMap.put("msg", "false");
		} else {
			resultMap.put("msg", "true");
		}
		return resultMap;

	}

	@ResponseBody
	@RequestMapping(value = "/ajaxPassword")
	public Map<String, Object> ajaxPassword(String registName,
			String oldPassword, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		if (StringUtil.isBlank(registName) || StringUtil.isBlank(oldPassword)) {
			resultMap.put("msg", "参数为空");
			return resultMap;
		}
		List<User> result = userService.findUserByRegistName(registName);
		if (result != null && result.size() > 0) {
			if (result.get(0).getPassword().trim().equals(oldPassword)) {
				resultMap.put("msg", "true");
			} else {
				resultMap.put("msg", "false");
			}

		} else {
			resultMap.put("msg", "找不到该用户数据");
		}

		return resultMap;

	}

	@RequestMapping("/addUser")
	public String addUser() {
		return "/server/user/add";
	}

	@RequestMapping("/saveUser")
	public String saveUser(User user, ModelMap model) {
		userService.saveUser(user);
		model.put("msg", "success");
		return "redirect:list.html"; 

	}

	@RequestMapping("/editUser")
	public String editUser(Long userId, ModelMap model) {
		User user = userService.findUserById(userId);
		model.put("user", user);
		model.put("userId", userId);
		return "/server/user/edit";
	}

	@RequestMapping("/updateUser")
	public String updateUser(User user, ModelMap model) {
		userService.updateUser(user);

		return "redirect:list.html";
	}

	@RequestMapping("/editUserPassword")
	public String editUserPassword(Long userId, ModelMap model) {
		User user = userService.findUserById(userId);
		model.put("user", user);
		model.put("userId", userId);
		return "/server/user/editPassword";
	}

	@RequestMapping("/updateUserPassword")
	public String updateUserPassword(User user, ModelMap model) {
		userService.updateUser(user);
		model.put("msg", "success");
		return "redirect:list.html";
	}

	@RequestMapping("deleteUser")
	@ResponseBody
	public Map<String, Object> deleteUser(Long userId, String userIdStr,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if (userId == null) {

				String[] tmp = userIdStr.split(",");
				Long[] userIds = new Long[tmp.length];
				for (int i = 0; i < tmp.length; i++) {
					userIds[i] = Long.parseLong(tmp[i]);
				}
				userService.deleteUser(userIds);
			} else {

				userService.deleteUser(new Long[] { userId });
			}
			resultMap.put("result", "删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", "删除失败！原因：" + e.toString());
		}
		return resultMap;
	}

}
